package com.mailsystem.www.controller;


import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;


/**
 * @author Administrator
 */
public abstract class BaseServlet extends HttpServlet {
    public void service(HttpServletRequest req, HttpServletResponse resp) {
        String methodName = req.getParameter("method");
        if (methodName == null || methodName.trim().isEmpty()) {
            throw new RuntimeException("您没有传递参数，无法确定你要调用的方法");
        }
            Class c = this.getClass();//得到当前类的class对象
        Method method = null;
        try {

            method = c.getMethod(methodName,
                    HttpServletRequest.class, HttpServletResponse.class);
        } catch (Exception e) {
            throw new RuntimeException("您要调用的方法：" + methodName + "(HttpServletRequest,HttpServletResponse)，它不存在！");
        }
        /*
         * 调用method表示的方法
         */
        try {
            String result = (String) method.invoke(this, req, resp);
        } catch (Exception e) {
            System.out.println("您调用的方法：" + methodName + ",　它内部抛出了异常！");
            throw new RuntimeException(e);
        }

    }
}

